package com.twittex.view
{	
	import com.twittex.TwittexFacade;
	import com.twittex.view.components.MainScreen;
	
	import flash.events.Event;
	
	import mx.events.FlexEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

    public class MainScreenMediator extends Mediator
    {
        public static const NAME:String = "MainScreenMediator";


        public function MainScreenMediator( viewComponent:MainScreen ) {
            super( NAME, viewComponent );
			// Wait for view components creation before register mediators
            mainScreen.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
        }
        
        /**
        * Called when mainScreen has been created, including all of its children
        */
        private function onCreationComplete(event:Event):void {
			facade.registerMediator(new UserScreenMediator(mainScreen.userScreen));
			facade.registerMediator(new FriendsScreenMediator(mainScreen.friendsScreen));
			facade.registerMediator(new RepliesScreenMediator(mainScreen.repliesScreen));
			facade.registerMediator(new StatusScreenMediator(mainScreen.statusScreen));
			facade.registerMediator(new NavigationScreenMediator(mainScreen.navigationScreen));
        }

        override public function listNotificationInterests():Array {
            return [ 
					TwittexFacade.VIEW_FRIENDS_SCREEN,
					TwittexFacade.VIEW_REPLIES_SCREEN
					];
        }

        override public function handleNotification( note:INotification ):void {
            switch ( note.getName() ) {
				case TwittexFacade.VIEW_FRIENDS_SCREEN:
					mainScreen.navViewStack.selectedChild = mainScreen.friendsScreen;
					break;
				case TwittexFacade.VIEW_REPLIES_SCREEN:
					mainScreen.navViewStack.selectedChild = mainScreen.repliesScreen;
					break;
            }
        }

        protected function get mainScreen():MainScreen {
        	return viewComponent as MainScreen;
        }
    }
}